package jspts.qotd.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class QuestionOfTheDay implements EntryPoint {
	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	private static final String SERVER_ERROR = "An error occurred while "
			+ "attempting to contact the server. Please check your network "
			+ "connection and try again.";

	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 */
	private final ServerConnectionsAsync greetingService = GWT
			.create(ServerConnections.class);

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		final Button sendButton = new Button("Send");
		final TextBox nameField = new TextBox();
		final Label lastQuestion = new Label();
		nameField.setText("question");
		sendButton.addStyleName("sendButton");
		RootPanel.get("nameFieldContainer").add(nameField);
		RootPanel.get("sendButtonContainer").add(sendButton);
		RootPanel.get("lastQuestionContainer").add(lastQuestion);
		nameField.setFocus(true);
		nameField.selectAll();

		// Load last question from Server
		greetingService.receiveQuestionFromServer(new AsyncCallback<String>() {
					public void onFailure(Throwable caught) {}
					public void onSuccess(String result) {
						lastQuestion.setText(result);
					}
				});
		
		final DialogBox dialogBox = new DialogBox();
		dialogBox.setText("Question posted");
		dialogBox.setAnimationEnabled(true);
		final Button closeButton = new Button("Close");
		final Label textToServerLabel = new Label("Thank you for your question!");
		VerticalPanel dialogVPanel = new VerticalPanel();
		dialogVPanel.add(textToServerLabel);
		dialogVPanel.add(closeButton);
		dialogBox.setWidget(dialogVPanel);
		closeButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				dialogBox.hide();
				sendButton.setEnabled(true);
				sendButton.setFocus(true);
			}
		});
		class MyHandler implements ClickHandler, KeyUpHandler {
			/**
			 * Fired when the user clicks on the sendButton.
			 */
			public void onClick(ClickEvent event) {
				updateLastQuestionServer(nameField.getText());
			}

			/**
			 * Fired when the user types in the nameField.
			 */
			public void onKeyUp(KeyUpEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					updateLastQuestionServer(nameField.getText());
				}
			}

			/**
			 * Send the name from the nameField to the server and wait for a response.
			 */
			private void updateLastQuestionServer(String input) {
				// First, we validate the input.
				lastQuestion.setText("");
				String textToServer = input;
				sendButton.setEnabled(false);
				textToServerLabel.setText(textToServer);
				greetingService.sendQuestionToServer(textToServer,
						new AsyncCallback<String>() {
							public void onFailure(Throwable caught) {}
							public void onSuccess(String result) {
								dialogBox.center();
								lastQuestion.setText(result);
								closeButton.setFocus(true);
							}
						});
			}
		}
		MyHandler handler = new MyHandler();
		sendButton.addClickHandler(handler);
		nameField.addKeyUpHandler(handler);
	}
}
